package com.sinosoft.product;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.GenericDaoHibernate;

@Repository
public class ProductDisplayDaoHibernate extends
		GenericDaoHibernate<ProductDisplay, Long> implements ProductDisplayDao {

	public ProductDisplayDaoHibernate() {
		super(ProductDisplay.class);
		// TODO Auto-generated constructor stub
	}

	private ArrayList<ProductDisplay> alist = null;

	private int count = 0;

	@SuppressWarnings("unchecked")
	public ArrayList<ProductDisplay> getProduct(final String sql,
			final HttpServletRequest request, final MessageSource messageSource) {

		// TODO Auto-generated method stub
		alist = new ArrayList();
		this.getSessionFactory().getCurrentSession().doWork(new Work() {
			ResultSet rs = null;
			Statement stmt = null;

			public void execute(Connection connection) throws SQLException {
				MessageSourceAccessor text = new MessageSourceAccessor(
						messageSource, request.getLocale());
				stmt = connection.createStatement(
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				// System.out.println(sql);
				rs = stmt.executeQuery(sql);
				while (rs != null && rs.next()) {
					String productId = rs.getString("PRODUCTID");
					String satelliteid = rs.getString("SATELLITEID");
					String sensorid = rs.getString("SENSORID");
					String centertime = rs.getString("CENTERTIME");
					String browsefilelocation = "/zywx3/BROWSE"
							+ rs.getString("BROWSEFILELOCATION");
					// System.out.println(browsefilelocation);
					String productlevel = rs.getString("PRODUCTLEVEL");
					ProductDisplay product = new ProductDisplay();
					product.setProductid(productId);
					product.setSatelliate(satelliteid);
					product.setSonser(sensorid);
					product.setScenedate(centertime);
					product.setBrowsefile(browsefilelocation);
					product.setProductlevel(productlevel);
					product.setDisplaystr(text
							.getMessage("collect.satelliteid")
							+ ":"
							+ satelliteid
							+ text.getMessage("collect.sensorid")
							+ ":"
							+ sensorid
							+ text.getMessage("product.collectdate")
							+ ":" + centertime);

					alist.add(product);
				}

			}

		});
		return alist;

		// Session session = null;
		// List<ProductDisplay> productList = null;
		// try {
		// session = this.getHibernateTemplate().getSessionFactory()
		// .openSession();
		// Query query = session.createSQLQuery(sql);
		// productList = query.list();
		//
		// } catch (Exception e) {
		// SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		// } finally {
		// session.close();
		// }
		// return productList;
	}

	@SuppressWarnings("unchecked")
	public int getCount(String sql) {
		// TODO Auto-generated method stub
		int pageCount = 0;
		Session session = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Query query = session.createSQLQuery(sql);
			List list = query.list();

			pageCount = Integer.valueOf(list.get(0).toString());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return pageCount;
	}

	public int getShopCarStatus(final String sql) {
		// TODO Auto-generated method stub
		this.getSessionFactory().getCurrentSession().doWork(new Work() {
			ResultSet rs = null;
			Statement stmt = null;

			public void execute(Connection connection) throws SQLException {
				// TODO Auto-generated method stub
				stmt = connection.createStatement(
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				rs = stmt.executeQuery(sql);
				while (rs != null && rs.next()) {
					String datacount = rs.getString("DATACOUNT");
					count = Integer.valueOf(datacount);
				}

			}

		});

		return count;
	}
	// @SuppressWarnings("unchecked")
	// public int getShopCarStatus(String sql) {
	// // TODO Auto-generated method stub
	// Session session = null;
	// List shopCar = null;
	// int length = 0;
	// try {
	//
	// session = this.getHibernateTemplate().getSessionFactory()
	// .openSession();
	// session.clear();
	// Query query = session.createSQLQuery(sql);
	// shopCar = query.list();
	// length = shopCar.size();
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// } finally {
	// session.close();
	// }
	// return length;
	// }
}
